function $(id) {
	if (id != undefined)
		return document.getElementById(id);
	else
		return undefined;
}
// 创建formItem的<div>兄弟标签,将消息放入其中
function createSibling(formItem, message) {
	var tip = "tip_" + formItem.name;
	var newNode = $(tip);
	if (newNode == null) {
		var sibling = formItem.nextSibling;
		var parent = formItem.parentNode;
		newNode = document.createElement("div");
		newNode.id = tip;
		parent.insertBefore(newNode, sibling);
	}
	
	newNode.innerHTML =  "<font size='2px' color='red' >"+message+"</font>";
	newNode.style.visibility = 'visible';
	newNode.style.display = 'inline';
	newNode.style.position = 'absolute';
	
	return $(tip);
}

//创建formItem的<div>兄弟标签,将消息放入其中
function createSibling2(formItem, message) {
	var tip = "tip_" + formItem.name;
	var newNode = $(tip);
	if (newNode == null) {
		formItem.outerHTML += "<div id="+tip+">"+message+"</div>"; 
	}
	
	return $(tip);
}
// 检测formItem不为空
function checkNotNull(formItem) {
	var message = "内容为空";
	//创建formItem的兄弟节点，显示消息
	var tip = createSibling(formItem, message);
	if (tip != null) {
		var reg = new RegExp("^(\\s|\\S)*\\S(\\s|\\S)*$");
		formItem.isValide = check(reg.test(formItem.value),tip);
	}
}

// 检测formItem长度是否合适 1-length
function checkLength(formItem, length) {
	var message = "长度过长,不得超过"+length;
	var tip = createSibling(formItem, message);
	if (tip != null) {
		var reg = new RegExp("^\\S{1," + length + "}$");
		formItem.isValide = check(reg.test(formItem.value),tip);
	}
}

function checkFixedLength(formItem){
	var string=formItem.value;
	var len=string.length;
	if(len!=10){
    var message="必须是10位数字";
	var tip=createSibling(formItem,message);
	if(tip!=null){	
	formItem.isValide = check(false,tip);
	}
	}
}

//检测 formItem内容是否符合email格式
function checkEmail(formItem) {
	var message = "Email格式错误:xx@xx.xx";
	var tip = createSibling(formItem, message);
	if (tip != null) {
		// 电子邮件格式：name@email.address
		var reg = new RegExp("^[A-Za-z0-9]+@([A-Za-z0-9]|(\.))+[A-Za-z0-9]+$");
		formItem.isValide = check(reg.test(formItem.value),tip);
	}
}

//检测formItem是否符合 电话号码格式
function checkTelephone(formItem) {
	var message = "Telephone格式错误：023-89011234 12345678911";
	var tip = createSibling(formItem, message);
	if (tip != null) {
		// 电话格式：028-85994797 13031122434
		var reg = new RegExp("^(\\d+ (-|\\d)+)|(\\d+)$");
		formItem.isValide = check(reg.test(formItem.value),tip);
	}
}

//检测formItem是否符合数字格式
function checkDigital(formItem) {
	var message = "不是数字：[0-9]";
	var tip = createSibling(formItem, message);
	if (tip != null) {
		var reg = new RegExp("^[0-9]+$");
		formItem.isValide = check(reg.test(formItem.value),tip);	
	}
}

//姓名必须为汉字
function isChinese(formItem){
	var message = "请输入中文名称！";
	var tip = createSibling(formItem, message);
	if (tip != null) {
		var reg = new RegExp("^[\u4E00-\u9FA5]*$");
		formItem.isValide = check(reg.test(formItem.value),tip);
	}
}

//检测formItem是否符合日期格式
function checkDate(formItem) {
	var messag = "日期格式错误：xxxx-xx-xx";
	var tip = createSibling(formItem, messag);
	if (tip != null) {
		// 日期格式:2009-01-01
		var reg = new RegExp("^\\d{4}-\\d{1,2}-\\d{1,2}$");
		formItem.isValide = check(reg.test(formItem.value),tip);
	}
}
//检测表项范围是否合法
function checkRange(formItem,low,high) {
	var messag = "数值超出范围："+low+"-"+high;
	var tip = createSibling(formItem, messag);
	if (tip != null) {
		// 日期格式:2009-01-01
		var reg = new RegExp("^\\d*$");
		formItem.isValide = check(reg.test(formItem.value) && (1*formItem.value >= low && 1*formItem.value <= high),tip);
	}
}


//:TO-DO:...
function checkWebSite(formItem) {
	var messag = "网址格式错误：http://hello.ok.ccn";
	var tip = createSibling(formItem, messag);
	if (tip != null) {
		// 日期格式:2009-01-01
		var reg = new RegExp("^http://[A-Za-z0-9]{1,}\.([A-Za-z0-9]|\.){0,}[A-Za-z0-9]{1,5}$");
		formItem.isValide = check(reg.test(formItem.value),tip);
	}
}

//:TO-DO:...
function checkMoney(formItem) {
	var messag = "金额格式错误：123.00";
	var tip = createSibling(formItem, messag);
	if (tip != null) {
		//钱数必须是数字
		var reg = new RegExp("^[0-9]+(\.){0,1}[0-9]+$");
		formItem.isValide = check(reg.test(formItem.value),tip);
	}
}

//检测表单所有表项，是否合法
function checkForm(form) {
	var e = form.elements;
	for (i = 0; i < e.length; i += 1) {
		
		if(e[i].type == 'text' || e[i].tagName == 'textarea')
		{
			if(e[i] == undefined)
				checkNotNull(e[i]);
		}
		if ( e[i].isValide != undefined && e[i].isValide == false) {
				return false;
		}
	}
	return true;
}

function check(condition,tip) {

	if (condition) {
		tip.style.visibility = 'hidden';
		return true;
	} else {
		tip.style.visibility = 'visible';
		return false;
	}
}

function checkTextAreaLength(object,min,max)
{
	var content = object.innerHTML;
	
	if (content.length > max){
		object.innerHTML = content.substr(0, max);
	}
}
function checkInputLength(object,min,max)
{
	
	var content = object.value;
	if(content.length>max){
		object.value = content.substr(0,max);
	}
}